package com.artron.mediaartron.ui.fragment;

import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;

import com.artron.baselib.base.BaseStaticFragment;
import com.artron.baselib.utils.UIUtils;
import com.artron.mediaartron.R;
import com.artron.mediaartron.ui.activity.MainActivity;
import com.artron.mediaartron.ui.widget.PagerIndicator;

import butterknife.BindView;

/**
 * Class description here
 *
 * @author doing
 * @version 1.0.0
 * @since 2017-12-06.
 */
public class StartTwoFragment extends BaseStaticFragment {

    @BindView(R.id.StartTwoFragment_pager)
    protected ViewPager mViewPager;
    @BindView(R.id.StartTwoFragment_btn_next)
    protected Button mBtnNext;
    @BindView(R.id.StartTwoFragment_pager_indicator)
    protected PagerIndicator mPagerIndicator;

    public static StartTwoFragment newInstance() {
        Bundle args = new Bundle();
        StartTwoFragment fragment = new StartTwoFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_start_two;
    }

    @Override
    protected void initView() {
        mViewPager.setAdapter(new PagerAdapter() {
            @Override
            public int getCount() {
                return 4;
            }

            @Override
            public boolean isViewFromObject(View view, Object object) {
                return view == object;
            }

            @Override
            public Object instantiateItem(ViewGroup container, int position) {
                ImageView iv = new ImageView(mContext);
                int image = UIUtils.getIdentifier("ic_start_two_page" + position, "drawable");
                iv.setImageResource(image);
                iv.setScaleType(ImageView.ScaleType.CENTER_CROP);
                iv.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                        ViewGroup.LayoutParams.MATCH_PARENT));
                container.addView(iv);
                return iv;
            }

            @Override
            public void destroyItem(ViewGroup container, int position, Object object) {
                container.removeView((View) object);
            }
        });
        mPagerIndicator.setViewPager(mViewPager);
        mViewPager.addOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
            @Override
            public void onPageSelected(int position) {
                if (position == 3) {
                    mBtnNext.setVisibility(View.VISIBLE);
                } else {
                    mBtnNext.setVisibility(View.GONE);
                }
            }
        });
        mBtnNext.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                MainActivity.start(mContext);
                getActivity().finish();
            }
        });
    }

}
